//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.xjrsoft.module.bi.common.tool;

import org.springframework.lang.Nullable;
import org.springframework.util.DigestUtils;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil extends DigestUtils {
    private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();

    public DigestUtil() {
    }

    public static String md5Hex(final String data) {
        return md5DigestAsHex(data.getBytes(Charsets.UTF_8));
    }

    public static String md5Hex(final byte[] bytes) {
        return md5DigestAsHex(bytes);
    }

    public static String sha1(String srcStr) {
        return hash("SHA-1", srcStr);
    }

    public static String sha256(String srcStr) {
        return hash("SHA-256", srcStr);
    }

    public static String sha384(String srcStr) {
        return hash("SHA-384", srcStr);
    }

    public static String sha512(String srcStr) {
        return hash("SHA-512", srcStr);
    }

    public static byte[] hmacMd5(String data, String key) {
        return hmacMd5(data.getBytes(Charsets.UTF_8), key);
    }

    public static byte[] hmacMd5(final byte[] bytes, String key) {
        return digestHmac("HmacMD5", bytes, key);
    }

    public static String hmacMd5Hex(String data, String key) {
        return encodeHex(hmacMd5(data.getBytes(Charsets.UTF_8), key));
    }

    public static String hmacMd5Hex(final byte[] bytes, String key) {
        return encodeHex(hmacMd5(bytes, key));
    }

    public static byte[] hmacSha1(String data, String key) {
        return hmacSha1(data.getBytes(Charsets.UTF_8), key);
    }

    public static byte[] hmacSha1(final byte[] bytes, String key) {
        return digestHmac("HmacSHA1", bytes, key);
    }

    public static String hmacSha1Hex(String data, String key) {
        return encodeHex(hmacSha1(data.getBytes(Charsets.UTF_8), key));
    }

    public static String hmacSha1Hex(final byte[] bytes, String key) {
        return encodeHex(hmacSha1(bytes, key));
    }

    public static byte[] hmacSha224(String data, String key) {
        return hmacSha224(data.getBytes(Charsets.UTF_8), key);
    }

    public static byte[] hmacSha224(final byte[] bytes, String key) {
        return digestHmac("HmacSHA224", bytes, key);
    }

    public static String hmacSha224Hex(String data, String key) {
        return encodeHex(hmacSha224(data.getBytes(Charsets.UTF_8), key));
    }

    public static String hmacSha224Hex(final byte[] bytes, String key) {
        return encodeHex(hmacSha224(bytes, key));
    }

    public static byte[] hmacSha256(String data, String key) {
        return hmacSha256(data.getBytes(Charsets.UTF_8), key);
    }

    public static byte[] hmacSha256(final byte[] bytes, String key) {
        return digestHmac("HmacSHA256", bytes, key);
    }

    public static String hmacSha256Hex(String data, String key) {
        return encodeHex(hmacSha256(data.getBytes(Charsets.UTF_8), key));
    }

    public static String hmacSha256Hex(final byte[] bytes, String key) {
        return encodeHex(hmacSha256(bytes, key));
    }

    public static byte[] hmacSha384(String data, String key) {
        return hmacSha384(data.getBytes(Charsets.UTF_8), key);
    }

    public static byte[] hmacSha384(final byte[] bytes, String key) {
        return digestHmac("HmacSHA384", bytes, key);
    }

    public static String hmacSha384Hex(String data, String key) {
        return encodeHex(hmacSha384(data.getBytes(Charsets.UTF_8), key));
    }

    public static String hmacSha384Hex(final byte[] bytes, String key) {
        return encodeHex(hmacSha384(bytes, key));
    }

    public static byte[] hmacSha512(String data, String key) {
        return hmacSha512(data.getBytes(Charsets.UTF_8), key);
    }

    public static byte[] hmacSha512(final byte[] bytes, String key) {
        return digestHmac("HmacSHA512", bytes, key);
    }

    public static String hmacSha512Hex(String data, String key) {
        return encodeHex(hmacSha512(data.getBytes(Charsets.UTF_8), key));
    }

    public static String hmacSha512Hex(final byte[] bytes, String key) {
        return encodeHex(hmacSha512(bytes, key));
    }

    public static String digestHmacHex(String algorithm, String text, String key) {
        return digestHmacHex(algorithm, text.getBytes(StandardCharsets.UTF_8), key);
    }

    public static String digestHmacHex(String algorithm, final byte[] bytes, String key) {
        return encodeHex(digestHmac(algorithm, bytes, key));
    }

    public static byte[] digestHmac(String algorithm, final byte[] bytes, String key) {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm);

        try {
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            return mac.doFinal(bytes);
        } catch (InvalidKeyException | NoSuchAlgorithmException var5) {
            throw Exceptions.unchecked(var5);
        }
    }

    public static String encodeHex(byte[] bytes) {
        return HexUtil.encodeToString(bytes);
    }

    public static byte[] decodeHex(final String hexStr) {
        return HexUtil.decode(hexStr);
    }

    public static String hash(String algorithm, String srcStr) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(srcStr.getBytes(Charsets.UTF_8));
            return toHex(bytes);
        } catch (NoSuchAlgorithmException var4) {
            throw Exceptions.unchecked(var4);
        }
    }

    public static String toHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);

        for(int i = 0; i < bytes.length; ++i) {
            ret.append(HEX_DIGITS[bytes[i] >> 4 & 15]);
            ret.append(HEX_DIGITS[bytes[i] & 15]);
        }

        return ret.toString();
    }

    public static boolean slowEquals(@Nullable String a, @Nullable String b) {
        return a != null && b != null ? slowEquals(a.getBytes(Charsets.UTF_8), b.getBytes(Charsets.UTF_8)) : false;
    }

    public static boolean slowEquals(@Nullable byte[] a, @Nullable byte[] b) {
        if (a != null && b != null) {
            if (a.length != b.length) {
                return false;
            } else {
                int diff = a.length ^ b.length;

                for(int i = 0; i < a.length && i < b.length; ++i) {
                    diff |= a[i] ^ b[i];
                }

                return diff == 0;
            }
        } else {
            return false;
        }
    }

    public static String encrypt(String data) {
        return sha1(md5Hex(data));
    }
}
